package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IUserTaskService;
import com.mdd.admin.validate.UserTaskCreateValidate;
import com.mdd.admin.validate.UserTaskUpdateValidate;
import com.mdd.admin.validate.UserTaskSearchValidate;
import com.mdd.admin.vo.UserTaskListedVo;
import com.mdd.admin.vo.UserTaskDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.UserTask;
import com.mdd.common.mapper.UserTaskMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户定时任务实现类
 * @author DEREK
 */
@Service
public class UserTaskServiceImpl implements IUserTaskService {
        
    @Resource
    UserTaskMapper userTaskMapper;

    /**
     * 用户定时任务列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<UserTaskListedVo>
     */
    @Override
    public PageResult<UserTaskListedVo> list(PageValidate pageValidate, UserTaskSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<UserTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        userTaskMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:type:int",
            "=:status:int",
            "like:title:str",
            "=:content:str",
            "=:extend:str",
        });

        IPage<UserTask> iPage = userTaskMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<UserTaskListedVo> list = new LinkedList<>();
        for(UserTask item : iPage.getRecords()) {
            UserTaskListedVo vo = new UserTaskListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 用户定时任务详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return UserTask
     */
    @Override
    public UserTaskDetailVo detail(Integer id) {
        UserTask model = userTaskMapper.selectOne(
                new QueryWrapper<UserTask>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        UserTaskDetailVo vo = new UserTaskDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 用户定时任务新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(UserTaskCreateValidate createValidate) {
        UserTask model = new UserTask();
        model.setUserId(createValidate.getUserId());
        model.setType(createValidate.getType());
        model.setStatus(createValidate.getStatus());
        model.setTitle(createValidate.getTitle());
        model.setContent(createValidate.getContent());
        model.setExtend(createValidate.getExtend());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userTaskMapper.insert(model);
    }

    /**
     * 用户定时任务编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(UserTaskUpdateValidate updateValidate) {
        UserTask model = userTaskMapper.selectOne(
                new QueryWrapper<UserTask>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setType(updateValidate.getType());
        model.setStatus(updateValidate.getStatus());
        model.setTitle(updateValidate.getTitle());
        model.setContent(updateValidate.getContent());
        model.setExtend(updateValidate.getExtend());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userTaskMapper.updateById(model);
    }

    /**
     * 用户定时任务删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        UserTask model = userTaskMapper.selectOne(
                new QueryWrapper<UserTask>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        userTaskMapper.delete(new QueryWrapper<UserTask>().eq("id", id));
    }

}
